package com.example.joke.custom.letter

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import com.example.joke.R

// TODO:  手指在view上面触摸的交互效果，屏幕中间显示当前选择的字母，及时回调暂未处理
class LetterSlideBar : View {

    private lateinit var mPaintNormal: Paint
    private lateinit var mPaintSelect: Paint
    private var textColorNormal = Color.BLACK
    private var textColorSelect = Color.BLUE
    private var textSize = 12
    private val mLetters = listOf("#", "A", "B", "C", "D", "E", "F", "G","H", "I", "J", "K", "L", "M",
        "N","O", "P", "Q", "R", "S", "T", "U","V", "W", "X", "Y", "Z")
    // 当前选中的字母
    private var mCurrentLetter = ""
    // 每个item的高度
    private var mItemHeight = 0
    private var mListener: ((String) -> Unit)? = null

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        val typedArray = context?.obtainStyledAttributes(attrs, R.styleable.LetterSlideBar)
        textColorNormal = typedArray?.getColor(R.styleable.LetterSlideBar_textColorNormal, textColorNormal)!!
        textColorSelect = typedArray?.getColor(R.styleable.LetterSlideBar_textColorSelect, textColorSelect)!!
        textSize = typedArray.getDimensionPixelSize(R.styleable.LetterSlideBar_letterTextSize, textSize)
        typedArray.recycle()

        initPaint()
    }

    private fun initPaint() {
        mPaintNormal = Paint()
        mPaintNormal.isDither = true
        mPaintNormal.isAntiAlias = true
        mPaintNormal.color = textColorNormal
        mPaintNormal.textSize = textSize.toFloat()

        mPaintSelect = Paint()
        mPaintSelect.isDither = true
        mPaintSelect.isAntiAlias = true
        mPaintSelect.color = textColorSelect
        mPaintSelect.textSize = textSize.toFloat()

    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val width = paddingLeft + paddingRight + mPaintNormal.measureText("A")
        val height = MeasureSpec.getSize(heightMeasureSpec)
        setMeasuredDimension(width.toInt(), height)
    }

    override fun onDraw(canvas: Canvas?) {
        // 计算每个字母item的高度
        mItemHeight = (height - paddingTop - paddingBottom) / mLetters.size
        val fontMetrics = mPaintNormal.fontMetrics
        val dy = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom
        val baseLine = mItemHeight / 2 + dy
        for (i in mLetters.indices) {
            val s = mLetters[i]
            // 不能用单独一个字母测宽度，因为字母宽度不同，eg：A和I宽度不同
            val x = width / 2 - mPaintNormal.measureText(s) / 2
            val y = paddingTop + i * mItemHeight + baseLine
            if (s == mCurrentLetter) {
                canvas?.drawText(s, x, y, mPaintSelect)
            } else {
                canvas?.drawText(s, x, y, mPaintNormal)
            }
        }
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        if (event?.action == MotionEvent.ACTION_MOVE) {
            // 计算触摸点的y坐标应该是什么字母
            var currentPosition = ((event.y - paddingTop) / mItemHeight).toInt()
            if (currentPosition < 0) {
                currentPosition = 0
            }
            if (currentPosition > mLetters.size - 1) {
                currentPosition = mLetters.size - 1
            }
            mCurrentLetter = mLetters[currentPosition]
            mListener?.invoke(mCurrentLetter)
            invalidate()
        }
        return true
    }

    fun setOnSelectCallBack(listener: (String) -> Unit) {
        this.mListener = listener
    }
}